<template>
  <div class="role-page" v-loading="loading" element-loading-text="加载中...">
    <div class="role-title">
      <div class="role-title-left">
        <el-select v-model="searchForm.status" clearable placeholder="状态">
          <el-option label="有效" :value="1" />
          <el-option label="无效" :value="0" />
        </el-select>
        <el-button style="margin-left: 10px" type="primary" @click="searchFun">查询</el-button>
      </div>
      <div class="role-title-right">
        <el-button type="primary" :icon="Plus" @click="addDeptFun('add')">增加角色</el-button>
      </div>
    </div>
    <div class="role-table">
      <el-table border stripe :data="tableData" :height="store.tableHeight">
        <el-table-column prop="name" label="角色名称" />
        <el-table-column prop="code" label="角色代码" />
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <!-- <el-tag v-if="row.status == 1" type="success">有效</el-tag>
            <el-tag v-else-if="row.status == 0" type="warning">无效</el-tag> -->
            <el-switch v-model="row.status" class="ml-2" width="60" inline-prompt @change="switchFun(row)" style="
                --el-switch-on-color: #13ce66;
                --el-switch-off-color: #ff4949;
              " :active-value="1" active-text="有效" :inactive-value="0" inactive-text="无效" />
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序权重" />
        <el-table-column prop="desc" label="备注说明" />
        <el-table-column prop="created_at" label="创建时间" />
        <el-table-column label="操作" width="190">
          <template #default="{ row }">
            <el-button type="primary" link @click="updateDeptFun('update', row)">
              编辑
            </el-button>
            <el-button type="warning" link @click="roleFun(row)">
              角色授权
            </el-button>
            <el-button type="danger" v-if="row.is_sys !== 1" link @click="delFun(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <addDialog :treeData="tableData" ref="deptAddRef" @refreshList="refreshList" />
  <!-- 授权 -->
  <DialogRoleEmpower ref="empowerRef" @refreshList="refreshList" />
</template>
<script setup>
import { Refresh, Plus } from "@element-plus/icons-vue";
import { useCounterStore } from "@/stores/counter.js";
import { listApi, stateApi, delApi } from "@/plugins/api/system/role";
import addDialog from "./components/DialogRole.vue";
import DialogRoleEmpower from "./components/DialogRoleEmpower.vue";
const store = useCounterStore();
let searchForm = ref({});
function searchFun() {
  tableData.value = [];
  getData();
}
let tableData = ref([]);
let loading = ref(false);
function getData() {
  loading.value = true;
  listApi(searchForm.value)
    .then((data) => {
      tableData.value = data;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}
let deptAddRef = ref(null);
function addDeptFun(type) {
  deptAddRef.value.initFun(type);
}
function updateDeptFun(type, row) {
  deptAddRef.value.initFun(type, row);
}
function refreshList() {
  getData();
}
function switchFun(row) {
  stateApi({
    id: row.id,
    status: row.status,
  }).then((data) => {
    ElMessage({
      type: "success",
      message: "状态修改成功！",
    });
    getData();
  });
}
function delFun(row) {
  ElMessageBox.confirm("确认删除，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi({
        id: row.id.toString(),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}
// 角色授权
let empowerRef = ref(null);
function roleFun(row) {
  empowerRef.value.initFun(row);
}

onMounted(() => {
  getData();
});
</script>
<style scoped lang="scss">
.role-page {
  width: 100%;
  height: 100%;

  .role-title {
    width: 100%;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #eeeeee;
    padding-bottom: 10px;

    .role-title-left {
      white-space: nowrap;
    }
  }

  .role-table {
    padding: 10px;
    box-sizing: border-box;
  }
}
</style>
